package org.jeecg.common.util.dingtalk;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.jeecg.common.constant.ProcessConstant;

import com.alibaba.fastjson.JSON;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiProcessinstanceCreateRequest;
import com.dingtalk.api.request.OapiProcessinstanceCreateRequest.FormComponentValueVo;
import com.dingtalk.api.response.OapiProcessinstanceCreateResponse;
import com.taobao.api.ApiException;

/**
 * 发起流程审批
 * @author mac
 *
 */
public class ProcessUtils {
	
	/**
	 * 发起物料出库审批实例
	 * process_code PROC-7BEEE34B-7A26-4227-91D5-312E7E9A62EF
	 * userID 035065345723610525
	 * 审批单图片地址：https://swwz.oss-cn-beijing.aliyuncs.com/dingtalk/WechatIMG62.jpeg
	 */
	public static void createActivity() {
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/create");
			OapiProcessinstanceCreateRequest req = new OapiProcessinstanceCreateRequest();
			req.setAgentId(836958846L);
			req.setProcessCode("PROC-7BEEE34B-7A26-4227-91D5-312E7E9A62EF");
			req.setOriginatorUserId("035065345723610525");
			req.setDeptId(-1L);
			List<FormComponentValueVo> list2 = new ArrayList<FormComponentValueVo>();
			FormComponentValueVo obj = new FormComponentValueVo();
			list2.add(obj);
			obj.setName("领用人");
			obj.setValue("张三");

			FormComponentValueVo obj2 = new FormComponentValueVo();
			list2.add(obj2);
			obj2.setName("类别");
			obj2.setValue("原材料");
			
			FormComponentValueVo obj3 = new FormComponentValueVo();
			list2.add(obj3);
			obj3.setName("领用日期");
			obj3.setValue("2020-08-11 13:51");
			
			/**开始拼接明细**/
		    FormComponentValueVo obj4 = new FormComponentValueVo();
		    obj4.setName("物料名称");
		    obj4.setValue("78克汇鑫玻璃卡面纸");
		    
		    FormComponentValueVo obj5 = new FormComponentValueVo();
		    obj5.setName("实领数量");
		    obj5.setValue("2122");
		    
		    FormComponentValueVo obj6 = new FormComponentValueVo();
		    obj6.setName("实际单价");
		    obj6.setValue("3.6");
		    
		    FormComponentValueVo obj7 = new FormComponentValueVo();
		    obj7.setName("计量单位");
		    obj7.setValue("平方");
			
		 // 明细
		    OapiProcessinstanceCreateRequest.FormComponentValueVo vo4 = new OapiProcessinstanceCreateRequest.FormComponentValueVo();
		    vo4.setName("物料明细");
		    vo4.setValue(JSON.toJSONString(Arrays.asList(Arrays.asList(obj4, obj5, obj6,obj7))));
		    list2.add(vo4);
			/**明细结束**/
			
			req.setFormComponentValues(list2);
			//获取token,真正发起调用
			OapiProcessinstanceCreateResponse rsp = client.execute(req, DingTalkUtils.get_token());
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @description 库存管理所有申请单，此处只是模拟使用钉钉接口，真正项目中需要配合小程序实现获取登录用户信息然后更新方法中setOriginatorUserId
	 * @param args
	 * @param process_code=PROC-DC326BC5-F99D-4D3B-A8C9-FD3464CCCCD3
	 * @return {"errcode":0,"process_instance_id":"2a094e5f-f1dc-4caa-b3fb-39c9ff0e96ce","request_id":"phqg6rhc8mjx"}
	 */
	public static void generateProduceApply() {
		
		try {
			DingTalkClient client = new DefaultDingTalkClient(ProcessConstant.URL_CREATEPROCESS);
			OapiProcessinstanceCreateRequest req = new OapiProcessinstanceCreateRequest();
			req.setAgentId(new Long(ProcessConstant.AGENTID));
			req.setProcessCode(ProcessConstant.KCPROCESSCODE);
			req.setOriginatorUserId("035065345723610525");
			req.setDeptId(-1L);
			List<FormComponentValueVo> list2 = new ArrayList<FormComponentValueVo>();
			FormComponentValueVo obj12 = new FormComponentValueVo();
			list2.add(obj12);
			obj12.setName("单据类型");
			obj12.setValue("采购入库");
			
			FormComponentValueVo obj13 = new FormComponentValueVo();
			list2.add(obj13);
			obj13.setName("业务员");
			obj13.setValue("张三");
			
			FormComponentValueVo obj14 = new FormComponentValueVo();
			list2.add(obj14);
			obj14.setName("经办人");
			obj14.setValue("李四");
			
			FormComponentValueVo obj = new FormComponentValueVo();
			list2.add(obj);
			obj.setName("申请时间");
			obj.setValue("2020-08-12 10:08");
			
			FormComponentValueVo obj15 = new FormComponentValueVo();
			list2.add(obj15);
			obj15.setName("原单类型");
			obj15.setValue("销售订单");
			
			FormComponentValueVo obj16 = new FormComponentValueVo();
			list2.add(obj16);
			obj16.setName("原单号");
			obj16.setValue("2020018899");
			
			FormComponentValueVo obj20 = new FormComponentValueVo();
			list2.add(obj20);
			obj20.setName("申请金额");
			obj20.setValue("300.256");

			FormComponentValueVo obj2 = new FormComponentValueVo();
			list2.add(obj2);
			obj2.setName("备注");
			obj2.setValue("这里填写备注信息");
			
//			/**开始拼接明细**/
//			//下拉
//		    FormComponentValueVo obj4 = new FormComponentValueVo();
//		    obj4.setName("货品名称");
//		    obj4.setValue("73克中档黄不打印（汇利）");
//		    
//		    //下拉
//		    FormComponentValueVo obj5 = new FormComponentValueVo();
//		    obj5.setName("计量单位");
//		    obj5.setValue("平方");
//		    
//		    FormComponentValueVo obj6 = new FormComponentValueVo();
//		    obj6.setName("入库数量");
//		    obj6.setValue("2123");
//		    
//		    FormComponentValueVo obj7 = new FormComponentValueVo();
//		    obj7.setName("入库成本");
//		    obj7.setValue("4.5");
//		    
//		    FormComponentValueVo obj8 = new FormComponentValueVo();
//		    obj8.setName("成本含税");
//		    obj8.setValue("是");
//		    
//		    FormComponentValueVo obj9 = new FormComponentValueVo();
//		    obj9.setName("仓库");
//		    obj9.setValue("0101仓库");
//		    
//		    FormComponentValueVo obj10 = new FormComponentValueVo();
//		    obj10.setName("批次号");
//		    obj10.setValue("202008120001");
//		    
//		    FormComponentValueVo obj17 = new FormComponentValueVo();
//		    obj17.setName("结算数量");
//		    obj17.setValue("200");
//		    
//		    FormComponentValueVo obj18 = new FormComponentValueVo();
//		    obj18.setName("含税单价");
//		    obj18.setValue("205");
//		    
//		    FormComponentValueVo obj19 = new FormComponentValueVo();
//		    obj19.setName("税率");
//		    obj19.setValue("0.01");
//		    
//		    FormComponentValueVo obj20 = new FormComponentValueVo();
//		    obj20.setName("税额");
//		    obj20.setValue("5");
//		    
//		    FormComponentValueVo obj21 = new FormComponentValueVo();
//		    obj21.setName("折让金额");
//		    obj21.setValue("5");
//		    
//		    FormComponentValueVo obj22 = new FormComponentValueVo();
//		    obj22.setName("折让税额");
//		    obj22.setValue("0.01");
//		    
//		    FormComponentValueVo obj11 = new FormComponentValueVo();
//		    obj11.setName("结算金额");
//		    obj11.setValue("200");
//			
//		    // 明细
//		    OapiProcessinstanceCreateRequest.FormComponentValueVo vo4 = new OapiProcessinstanceCreateRequest.FormComponentValueVo();
//		    vo4.setName("采购明细");
//		    vo4.setValue(JSON.toJSONString(Arrays.asList(Arrays.asList(obj4, obj5, obj6,obj7,obj8,obj9,obj10,obj17,obj18,obj19,obj20,obj21,obj22,obj11))));
//		    list2.add(vo4);
//			/**明细结束**/
			
			req.setFormComponentValues(list2);
			//获取token,真正发起调用  a1bb9cccbe2034f5848202a7391b4d7d
			OapiProcessinstanceCreateResponse rsp = client.execute(req, DingTalkUtils.get_token());
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * @description 往来管理所有申请单，此处只是模拟使用钉钉接口，真正项目中需要配合小程序实现获取登录用户信息然后更新方法中setOriginatorUserId
	 * @param args
	 * @param process_code=PROC-5A61F4A2-FAAA-4721-9097-0D86B7DA42EE
	 * @return {"errcode":0,"process_instance_id":"80cd548f-a31b-4d47-9a36-510d9d5b4bd3","request_id":"6osy7209pm6f"}
	 */
	public static void generateLProduceApply() {
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/create");
			OapiProcessinstanceCreateRequest req = new OapiProcessinstanceCreateRequest();
			req.setAgentId(836958846L);
			req.setProcessCode(ProcessConstant.WLPROCESSCODE);
			req.setOriginatorUserId("035065345723610525");
			req.setDeptId(-1L);
			List<FormComponentValueVo> list2 = new ArrayList<FormComponentValueVo>();
			FormComponentValueVo obj12 = new FormComponentValueVo();
			list2.add(obj12);
			obj12.setName("单据类型");
			obj12.setValue("销售预收");
			
			FormComponentValueVo obj = new FormComponentValueVo();
			list2.add(obj);
			obj.setName("申请时间");
			obj.setValue("2020-08-12 10:08");
			
			FormComponentValueVo obj15 = new FormComponentValueVo();
			list2.add(obj15);
			obj15.setName("供应商");
			obj15.setValue("公司名称1");
			
			FormComponentValueVo obj2 = new FormComponentValueVo();
			list2.add(obj2);
			obj2.setName("备注");
			obj2.setValue("这里填写备注信息");
			
			/**开始拼接明细**/
		    FormComponentValueVo obj4 = new FormComponentValueVo();
		    obj4.setName("源单分录号");
		    obj4.setValue("202008140001");
		    
		    //下拉
		    FormComponentValueVo obj5 = new FormComponentValueVo();
		    obj5.setName("结算方式");
		    obj5.setValue("转账");
		    
		    FormComponentValueVo obj6 = new FormComponentValueVo();
		    obj6.setName("资金账户");
		    obj6.setValue("62210022");
		    
		    FormComponentValueVo obj7 = new FormComponentValueVo();
		    obj7.setName("金额");
		    obj7.setValue("4.5");
		    
		    FormComponentValueVo obj8 = new FormComponentValueVo();
		    obj8.setName("明细备注");
		    obj8.setValue("这是明细备注");
		    
		    // 明细
		    OapiProcessinstanceCreateRequest.FormComponentValueVo vo4 = new OapiProcessinstanceCreateRequest.FormComponentValueVo();
		    vo4.setName("明细");
		    vo4.setValue(JSON.toJSONString(Arrays.asList(Arrays.asList(obj4, obj5, obj6,obj7,obj8))));
		    list2.add(vo4);
			/**明细结束**/
			
			req.setFormComponentValues(list2);
			//获取token,真正发起调用  a1bb9cccbe2034f5848202a7391b4d7d
			OapiProcessinstanceCreateResponse rsp = client.execute(req, DingTalkUtils.get_token());
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
		
	}
	
	public static void main(String[] args) {
		//成功提示：{"errcode":0,"process_instance_id":"37056a6f-8c51-44a0-bf3b-bc7b266f8124","request_id":"zpud1zg2pvn2"}
		generateLProduceApply();
	}

}
